package com.zxq.web.head.controller;

import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zxq.common.controller.BaseController;
import com.zxq.common.pojo.ApiResponse;
import com.zxq.common.pojo.ApiResponseEnum;
import com.zxq.common.util.ConstatFinalUtil;
import com.zxq.users.pojo.APosition;
import com.zxq.users.pojo.APositionEnum;
import com.zxq.users.pojo.AUsers;
import com.zxq.users.service.IUsersDbService;

/**
 * 	用户首页操作
 * 	@author wangsh
 */
@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main--");
		return "/head/users/main";
	}
	
	/**
	 * 欢迎页面
	 * @return
	 */
	@RequestMapping("/welcome")
	public String welcome()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--welcome--");
		return "/head/users/welcome";
	}
	
	/**
	 * 打开用户更新
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request,String updateFlag ,Model model)
	{
		
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdate--");
		String operType = request.getParameter("operType");
		/* 从session中获取信息 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 准备请求的信息 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId() + "");
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		
		/* 清除信息 */
		if(updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		model.addAttribute("response", apiResponse.toJSON());
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 查询所有的职位 */
			condMap.clear();
			/* 只查询启用的 */
			condMap.put("status", APositionEnum.STATUS_ENABLE.getStatus());
			/* 查询数据库
			 * 所有的数据以JSON的形式返回
			 *  */
			ApiResponse<APosition> response = this.usersDbService.findCondListPositionService(null, condMap);
			/* 将结果存储到Request中 */
			request.setAttribute("positionList", response.toJSON());
			
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateInfo";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePass";
		}else if("updateEmail".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateEmail";
		}else if("updatePhoto".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePhoto";
		}
		return "/head/users/usersInfo";
	}
	
	/**
	 * 打开用户提交
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdateSubmit--");
		ApiResponse<Object> apiResponseRes = new ApiResponse<Object>();
		
		String operType = request.getParameter("operType");
		/* 从session中获取信息 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		
		/* 准备请求的信息 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", users.getId() + "");
		ApiResponse<AUsers> usersApiResponse = this.usersDbService.findOneUsersService(condMap);
		users = usersApiResponse.getDataOneJava();
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 用户填写的信息 */
			String positionId = request.getParameter("positionId");
			String nickName = request.getParameter("nickName");
			String trueName = request.getParameter("trueName");
			String address = request.getParameter("address");
			String phone = request.getParameter("phone");
			String sex = request.getParameter("sex");
			
			users.setPositionId(Integer.valueOf(positionId));
			users.setNickName(nickName);
			users.setTrueName(trueName);
			users.setAddress(address);
			users.setPhone(phone);
			users.setSex(Byte.valueOf(sex));
			
			ApiResponse<Object> apiResponse = this.usersDbService.updateOneUsersService(users);
			model.addAttribute("response", apiResponse.toJSON());
			/* 根据不同的请求跳转不同的页面 */
			request.getSession().setAttribute("users", users);
			return "/head/users/usersUpdateInfo";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			String souPass = request.getParameter("souPass");
			String password = request.getParameter("password");
			
			if(this.encryptUtil.checkStr(souPass, users.getPassword()))
			{
				users.setPassword(this.encryptUtil.encodeStr(password));
				ApiResponse<Object> apiResponse = this.usersDbService.updateOneUsersService(users);
				model.addAttribute("response", apiResponse.toJSON());
				request.getSession().setAttribute("users", users);
			}else
			{
				apiResponseRes.setCode(ApiResponseEnum.INFO_SOUPASS_INCORRECT.getStatus());
				apiResponseRes.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponseRes.getCode() + ""),Collections.emptyMap());
				model.addAttribute("response", apiResponseRes.toJSON());
			}
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePass";
		}else if("updateEmail".equalsIgnoreCase(operType))
		{
			String email = request.getParameter("email");
			
			condMap.put("email", email);
			
			ApiResponse<Object> apiResponse = this.usersDbService.updateOneUsersService(users);
			model.addAttribute("response", apiResponse.toJSON());
			request.getSession().setAttribute("users", users);
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateEmail";
		}
		return "/head/users/usersInfo";
	}
	
	/**
	 * 退出页面
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		String returnUrl = request.getParameter("returnUrl");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取用户 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		if(returnUrl == null)
		{
			returnUrl = "" ; 
		}
		try
		{
			returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
			/* 做一些清除的操作 */
			session.removeAttribute("users");
			session.removeAttribute("lastLoginTime");
			
			apiResponse.setCode(ApiResponseEnum.INFO_LOGOUT.getStatus()); 
		} catch (Exception e)
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus()); 
			ConstatFinalUtil.SYS_LOGGER.error("退出报错了;email:{}", users.getEmail(),e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		/* 提示信息 */
		session.setAttribute("response", apiResponse.toJSON());
		/* 客户端跳转到登陆页面 */
		return "redirect:/headLogin.htm?returnUrl=" + returnUrl;
	}
}
